package handlers

import (
	"github.com/gin-gonic/gin"
	"month3/api/request"
	"month3/dao"
	"net/http"
)

// 发布动态
func AddArticle(c *gin.Context) {
	var data request.AddArticleReq
	err := c.ShouldBind(&data)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"Code": 500,
			"Msg":  "参数错误",
			"Data": nil,
		})
		return
	}

	if data.ArticleType < 1 && data.ArticleType > 3 {
		c.JSON(http.StatusOK, gin.H{
			"Code": 500,
			"Msg":  "支持发布文本、图片、视频等内容",
			"Data": nil,
		})
		return
	}

	result := dao.AddArticle(c.GetUint("userId"), data.Context, data.ArticleType, data.GoodNum, data.CommentNum)
	c.JSON(http.StatusOK, gin.H{
		"Code": 200,
		"Msg":  "成功",
		"Data": result,
	})
}

func List(c *gin.Context) {
	c.JSON(http.StatusOK, gin.H{
		"Code": 200,
		"Msg":  "成功",
		"Data": dao.List(),
	})
}

func Focus(c *gin.Context) {
	var data request.FocusReq
	err := c.ShouldBind(&data)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"Code": 500,
			"Msg":  "参数错误",
			"Data": nil,
		})
		return
	}

	result := dao.Focus(c.GetUint("userId"), data.FocusId)

	c.JSON(http.StatusOK, gin.H{
		"Code": 200,
		"Msg":  "成功",
		"Data": result,
	})
}

func List1(c *gin.Context) {
	userId := c.GetUint("userId")
	c.JSON(http.StatusOK, gin.H{
		"Code": 200,
		"Msg":  "成功",
		"Data": dao.List1(int(userId)),
	})
}

func UpdateArticleLike(c *gin.Context) {
	var data request.AddArticleLikeReq
	err := c.ShouldBind(&data)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"Code": 500,
			"Msg":  "参数错误",
			"Data": nil,
		})
		return
	}

	if data.Option == 1 {
		dao.AddArticleLike(data.ArticleId) //点赞
	} else {
		dao.DelArticleLike(data.ArticleId) //取消点赞
	}

	c.JSON(http.StatusOK, gin.H{
		"Code": 200,
		"Msg":  "成功",
		"Data": true,
	})
}

func AddComment(c *gin.Context) {
	var data request.AddCommentReq
	err := c.ShouldBind(&data)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"Code": 500,
			"Msg":  "参数错误",
			"Data": nil,
		})
		return
	}

	userId := c.GetUint("userId")
	if data.Option == 1 {
		dao.AddComment(data.ArticleId, data.Content, int(userId)) //添加评论
	} else {
		dao.AppendComment(data.ArticleId, data.Content, int(userId)) //回复评论
	}

	c.JSON(http.StatusOK, gin.H{
		"Code": 200,
		"Msg":  "成功",
		"Data": true,
	})
}

func AddCommentLike(c *gin.Context) {
	var data request.AddCommentLikeReq
	err := c.ShouldBind(&data)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"Code": 500,
			"Msg":  "参数错误",
			"Data": nil,
		})
		return
	}
	if data.Option == 1 {
		dao.AddCommentLike(data.CommentId) //评论点赞
	} else {
		dao.DelAddCommentLike(data.CommentId) //评论取消点赞
	}

	c.JSON(http.StatusOK, gin.H{
		"Code": 200,
		"Msg":  "成功",
		"Data": true,
	})
}

func History(c *gin.Context) {
	var data request.HistoryReq
	err := c.ShouldBind(&data)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"Code": 500,
			"Msg":  "参数错误",
			"Data": nil,
		})
		return
	}

	if data.Option == "like" {
		list1 := dao.ArticleLikeList()
		c.JSON(http.StatusOK, gin.H{
			"Code": 200,
			"Msg":  "成功",
			"Data": list1,
		})
	} else {
		list2 := dao.CommentLikeList()
		c.JSON(http.StatusOK, gin.H{
			"Code": 200,
			"Msg":  "成功",
			"Data": list2,
		})
	}
}
